# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=waf
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.0.20
pkgrel=1
pkgdesc="General-purpose build system modelled after Scons (mingw-w64)"
arch=('any')
license=('BSD')
url="https://waf.io/"
makedepends=("setconf")
depends=("${MINGW_PACKAGE_PREFIX}-python")
options=('!emptydirs')
source=("https://waf.io/${_realname}-${pkgver}.tar.bz2"{,.asc})
validpgpkeys=('8AF22DE5A06822E3474F3C7049B4C67C05277AAA')
sha256sums=('cce635c2d1a0f93b4f5b811e0452a35b8066e8ccd78632447f5be24c4c3f1a63'
            'SKIP')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  # Extracting license
  head -n 30 waf-light | tail -n 25 > LICENSE
  # Python 3 fix
  sed -i '0,/env python/s//python/' waf-light
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"
  plain "Configure ..."
  ${MINGW_PREFIX}/bin/python ./waf-light configure --prefix=${MINGW_PREFIX}
  plain "Make waf ..."
  ${MINGW_PREFIX}/bin/python ./waf-light --make-waf
}

package() {
  cd "${srcdir}/${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/python ./waf-light install -f --destdir="${pkgdir}" \
    --tools='compat,compat15,ocaml,go,cython,scala,erlang,cuda,gcj,boost,pep8,eclipse,unity,clang_compilation_database'

  install -Dm755 waf "${pkgdir}${MINGW_PREFIX}/bin/waf"

  # Force the generation of .waf.admin files
  cd demos/c
  "${pkgdir}${MINGW_PREFIX}/bin/waf" configure build >& /dev/null
  cd ../..

  # Fix weird directory placement (FS#38216, FS#38300)
  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/waf"
  mv "${pkgdir}${MINGW_PREFIX}/bin/.waf3-${pkgver}-"* "${pkgdir}${MINGW_PREFIX}/lib/waf/"
  setconf "${pkgdir}${MINGW_PREFIX}/bin/waf" base '"${MINGW_PREFIX}/lib/waf"'

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

}
